1 // Equipo Poncho, Carriel y Ruana
22 template <class T
> string
toStr(const T
&x
)
23 { stringstream s
; s
<< x
; return s
.str(); }
24 template <class T
> int toInt(const T
&x
)
25 { stringstream s
; s
<< x
; int r
; s
>> r
; return r
; }
27 #define For(i, a, b) for (int i=(a); i<(b); ++i)
28 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
29 #define D(x) cout << #x " = " << (x) << endl;
31 const double EPS
= 1e-9;
33 int cmp(double x
, double y
= 0, double tol
= EPS
) {
34 return (x
<= y
+ tol
) ? (x
+ tol
< y
) ? -1 : 0 : 1;
37 #define INPUT_FILE "compression"
39 const int MAXBASES
= 105, oo
= 1 << 28;
40 set
<int> bases
[MAXBASES
];
43 int bit
[20]; // bit[i] = index of term i in the document
45 int memo
[MAXBASES
][1 << 16];
48 bool compatible(const set
<int> &base
) {
51 if (bit
[term
] == -1) return false; // base tiene un elemento que el doc no tiene
56 int maskOfBase(const set
<int> &base
) {
60 ans
= ans
| (1 << bit
[term
]);
65 int f(int b
, int mask
, const set
<int> &doc
) {
68 if (b
== B
) { // Vi todas las bases
69 return (mask
== ((1 << n
) - 1) ? 0 : oo
);
72 //printf("b = %d\n", b);
73 if (memo
[b
][mask
] != -1) return memo
[b
][mask
];
75 if (!compatible(bases
[b
])) {
76 return memo
[b
][mask
] = f(b
+ 1, mask
, doc
);
79 //assert(compatible(bases[b]));
82 int ans
= f(b
+ 1, mask
| maskOfBase(bases
[b
]), doc
) + 1;
86 ans
= min(ans
, f(b
+ 1, mask
, doc
));
87 return memo
[b
][mask
] = ans
;
90 void solve(const set
<int> &doc
) {
91 //puts("doc: "); foreach(x, doc) printf("%d ", *x); puts("");
92 memset(bit
, -1, sizeof bit
);
100 //For(i, 0, B) if (compatible(bases[i])) printf("Base i=%d: %d\n", i, maskOfBase(bases[i]));
102 // for (int i = 0; i < 16; ++i) {
103 // printf("bit[%d] = %d\n", i, bit[i]);
106 int ans
= f(0, 0, doc
);
107 if (ans
>= oo
) ans
= 0;
112 freopen(INPUT_FILE
".in", "r", stdin
);
113 while (cin
>> B
>> D
) {
114 if (B
== 0 and D
== 0) break;
115 for (int i
= 0; i
< B
; ++i
) {
119 int x
; cin
>> x
; x
--;
124 for (int j
= 0; j
< D
; ++j
) {
128 int x
; cin
>> x
; x
--;
131 memset(memo
, -1, sizeof memo
);
133 if (j
+ 1 < D
) printf(" ");